#!/bin/bash

function _build_completion()
{
	if [ "${COMP_CWORD}" = "1" ]
	then
		local cur prev
		_get_comp_words_by_ref cur prev
		COMPREPLY=($(compgen -W "$*" -- ${cur}))
	else
		_filedir
	fi
}

function _cavan_completion()
{
	_build_completion $(ls ${CAVAN_HOME}/app/*.c | sed 's#.*/\(.*\)\.c$#\1#g')
}

complete -F _cavan_completion cavan cavan-main

function _cavan_svn_repo()
{
	_build_completion "init clone update sync command cmd"
}

complete -F _cavan_svn_repo cavan-svn-repo svn-repo.py

function _cavan_git_svn_repo()
{
	_build_completion "init clone update sync command cmd backup clean cleanup symlink link ln recovery co checkout merge pull fetch"
}

complete -F _cavan_git_svn_repo cavan-git-svn-repo git-svn-repo.py

function _cavan_git_svn()
{
	_build_completion "init clone update sync rebase dcommit clean cleanup ln link symlink push co checkout merge"
}

complete -F _cavan_git_svn cavan-git-svn git-svn.py

function _adb_completion()
{
	local cur prev

	_get_comp_words_by_ref cur prev

	case ${COMP_CWORD} in
		1)
			COMPREPLY=($(compgen -W "devices connect disconnect push pull sync shell
				emu logcat forward jdwp install uninstall bugreport backup restore
				help version wait-for-device start-server kill-server get-state get-serialno
				status-window remount reboot reboot-bootloader root usb tcpip ppp" -- ${cur}))
			;;

		2)
			if [ "${prev}" = "shell" ]
			then
				compopt -o filenames
				COMPREPLY=($(compgen -dc -- "$cur"))
			else
				_filedir
			fi
			;;

		*)
			if [ "${prev}" = "busybox" ]
			then
				compopt -o filenames
				COMPREPLY=($(compgen -dc -- "$cur"))
			else
				_filedir
			fi
			;;
	esac
}

complete -F _adb_completion adb

function _cavan_make_completion()
{
	local cur prev

	_get_comp_words_by_ref cur prev

	case ${COMP_CWORD} in
		1)
			COMPREPLY=($(compgen -W "lib app" -- ${cur}))
			;;

		2)
			_filedir
			;;

		3|4)
			if [ "${COMP_WORDS[1]}" = "app" -a "${COMP_CWORD}" = 3 ]
			then
				COMPREPLY=($(compgen -W "simple normal" -- ${cur}))
			else
				COMPREPLY=($(compgen -W "c cpp" -- ${cur}))
			fi
			;;

		*)
			COMPREPLY=()
			;;
	esac
}

complete -F _cavan_make_completion cavan-make

function _fastboot_completion()
{
	local cur prev

	_get_comp_words_by_ref cur prev

	case ${COMP_CWORD} in
		1)
			COMPREPLY=($(compgen -W "update flashall flash erase format getvar boot flash:raw devices continue reboot reboot-bootloader help" -- ${cur}))
			;;
		2)
			case ${COMP_WORDS[1]} in
				flash | erase | format)
					COMPREPLY=($(compgen -W "
						2ndbl boot fixnv kpanic modemarm persist ramdisk_recovery simlock uboot
						abl1 boot_logo fota lcboot modemdsp0 productinfo recovery spl udisk
						aboot cache intmemory logo modemdsp1 ramdisk rpm splash userdata
						amt dsp kernel misc panic ramdisk_amt1 runtimenv system vmjaluna
						backupfixnv fastboot_logo kernel_recovery modem params ramdisk_amt3 sdi tz sbl1 usbmsc wcnss
						" -- ${cur}))
					;;
				update | boot)
					_filedir
					;;
				*)
					COMPREPLY=()
					;;
			esac
			;;
		*)
			_filedir
			;;
	esac
}

complete -F _fastboot_completion fastboot

function _build_helper_completion()
{
	_build_completion "kernelconfig kernelbuild bootimage mkboot ramdisk recovery lk all help"
}

complete -F _build_helper_completion build_helper.sh

function _git_repo_completion()
{
	_build_completion "abandon branch branches checkout cherry-pick diff download grep init list prune rebase smartsync stage start status sync upload"
}

complete -F _git_repo_completion repo git-repo
complete -F _command cavan-sudo cavan-service cavan-daemon-command

function _cavan_alarm()
{
	local cur prev

	_get_comp_words_by_ref cur prev

	case "${COMP_CWORD}" in
		1)
			COMPREPLY=($(compgen -W "add remove list" -- ${cur}))
			;;
		2)
			if [ "${COMP_WORDS[1]}" = "add" ]
			then
				compopt -o filenames
				COMPREPLY=($(compgen -dc -- "$cur"))
			else
				COMPREPLY=()
			fi
			;;
		*)
			if [ "${COMP_WORDS[1]}" = "add" ]
			then
				_command
			else
				COMPREPLY=()
			fi
	esac
}

complete -F _cavan_alarm cavan-alarm

function _cavan_config()
{
	_build_completion "host x86 arm board"
}

complete -F _cavan_config cavan-config-sh

function _cavan_set_jdk_version()
{
	local jdk jdks

	for fn in /tools/jdk*
	do
		[ -d "${fn}" ] || continue
		jdk=$(basename ${fn})
		[ "${jdk}" = "jdk" ] && continue
		jdks="${jdk} ${jdks}"
	done

	_build_completion "${jdks}"
}

complete -F _cavan_set_jdk_version cavan-set-jdk-version

function _cavan_dd()
{
	local cur prev words cword
	_init_completion -n = || return

	case $cur in
		if=*|of=*)
		cur=${cur#*=}
		_filedir
		return 0
		;;
	esac

	_expand || return 0

	COMPREPLY=($(compgen -W '$( _parse_help "$1" )' -- "$cur") \
		$(compgen -W 'bs count if of seek skip' -S '=' -- "$cur"))
}

complete -F _cavan_dd -o nospace cavan-dd
